<?php
class Debug
{
    private static $_timer;
    
    public static function ddump($vars)
    {
        $args = func_get_args();
        call_user_func_array(array('self', 'dump'), $args);
        exit();
    }

    public static function ddumpex($vars)
    {
        throw new Exception(self::_dump(func_get_args()));
    }

    public static function dump($vars)
    {
        echo self::_dump(func_get_args());
    }

    public static function _dump($args)
    {
        $args = func_get_args();
        $backtrace = debug_backtrace();
        $level = 1;
        $trace = "";
        foreach ($backtrace as $rec) {
            $trace .= "\n" . str_pad($level, 3, " ", STR_PAD_LEFT)
                   . ': ' . @$rec['file'] . ' (' . @$rec['line'] . ') - ';

            if (isset($rec['function'])) {
                if (isset($rec['class'])) {
                    $trace .= $rec['class'] . $rec['type'] . $rec['function'];
                } else {
                     $trace .= $rec['function'];
                }
                $trace .= '(';
                if (count($rec['args'])) {
                    foreach ($rec['args'] as $arg) {
                        if (is_object($arg)) {
                            $trace .= get_class($arg);
                        } elseif(is_array($arg)) {
                            $trace .= gettype($arg);
                        } elseif(is_bool($arg)) {
                            $trace .= $arg ? 'true' : 'false';
                        } elseif(is_null($arg)) {
                            $trace .= 'null';
                        } elseif (is_string($arg)) {
                            $arg = strlen($arg) <= 30 ? $arg : (substr($arg, 0, 25) . '[...]');
                            $arg = str_replace("\n", '\n', (string) $arg);
                            $trace .= '"' . $arg . '"';
                        } else {
                            $trace .= $arg;
                        }
                        $trace .= ', ';
                    }
                    $trace = substr($trace, 0, -2);
                }
                $trace .= ")<br />";
            }

            $level++;
        }
        $text = "<hr />" . $trace . "<pre>";
        foreach ($args as $var){
            $text .= gettype($var) . ":" . print_r($var, true) . "<br />";
        }
        $text .= '</pre><hr />';
        return $text;
    }

    public static function startTimer()
    {
        self::$_timer = microtime(true);
    }

    public static function checkTimer()
    {
        return microtime(true) - self::$_timer;
    }
}